package com.example.cvte.controller;

import cn.hutool.crypto.digest.BCrypt;
import com.example.cvte.util.VerificationCode;
import com.example.cvte.model.UserInfo;
import com.example.cvte.service.UserService;
import com.example.cvte.util.ConstVariable;
import com.example.cvte.util.WordFilter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author 郝文雪
 * @date 2022/10/31 13:53
 * @Description Test
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;


    /**
     * 用户注册功能实现
     * @param username 用户名
     * @param nickname 昵称
     * @param password 密码
     * @return
     */
    @RequestMapping("/reg")
    public boolean register(String username,String nickname,String password,String code,HttpSession session){
       String sessionCode=session.getAttribute("verify_code").toString();
        if(StringUtils.hasLength(code) && sessionCode.equalsIgnoreCase(code)) {
            if (StringUtils.hasLength(username) && StringUtils.hasLength(nickname) && StringUtils.hasLength(password)) {
                UserInfo userinfo = userService.getUserByUsername(username);
                if (userinfo != null) {
                    //用户已存在
                    return false;
                } else {
                    String words = WordFilter.replaceWords(nickname);
                    if (nickname.equals(words)) {
                        password = BCrypt.hashpw(password, BCrypt.gensalt());
                        int data = userService.addUser(username, nickname, password);
                        System.out.println(data);
                        return data == 1;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 用户登录功能的实现
     * @param username 用户名
     * @param password 密码
     * @param req 请求
     * @return
     */
    @RequestMapping("/login")
    public UserInfo login(String username, String password, String code ,HttpSession session,HttpServletRequest req){
        UserInfo userinfo=null;
        String sessionCode=session.getAttribute("verify_code").toString();
        if(StringUtils.hasLength(code) && sessionCode.equalsIgnoreCase(code)) {
            if (StringUtils.hasLength(username) && StringUtils.hasLength(password)) {
                userinfo = userService.getUserByUsername(username);
                if (userinfo != null) {
                    //比对密码是否正确
                    if (BCrypt.checkpw(password, userinfo.getPassword())) {
                        //密码正确，登录成功，设置session
                        HttpSession session1 = req.getSession(true);
                        session1.setAttribute(ConstVariable.USER_SESSION_KEY, userinfo);
                    } else {
                        userinfo = null;
                    }
                }
            }
        }
        return userinfo;
    }

    /**
     * 判断用户是否登录
     * @param req 获取请求
     * @return
     */
    @RequestMapping("/islogin")
    public UserInfo isLogin(HttpServletRequest req){
        UserInfo userinfo=null;
        HttpSession session=req.getSession(false);
        userinfo= (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
        return userinfo;
    }

    /**
     * 退出登录
     * @param req 请求
     * @return
     */
    @RequestMapping("/logout")
    public Boolean loginout(HttpServletRequest req){
        HttpSession session=req.getSession(false);
        if(session!=null && session.getAttribute(ConstVariable.USER_SESSION_KEY)!=null){
            session.removeAttribute(ConstVariable.USER_SESSION_KEY);
            return true;
        }
        return false;
    }

    /**
     * 生成验证码并返回给浏览器
     * @param request 请求
     * @param resp 返回
     * @throws IOException
     */
    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        VerificationCode code = new VerificationCode();
        BufferedImage image = code.getImage();
        String text = code.getText();
        HttpSession session = request.getSession(true);
        session.setAttribute("verify_code", text);
        VerificationCode.output(image,resp.getOutputStream());
    }
}
